package com.cy.service.Impl;

import com.cy.entity.JsonResult;
import com.cy.exception.enums.AppHttpCodeEnum;

import com.cy.service.UploadService;

import com.cy.utils.PathUtils;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;

import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@Service
@Data
@ConfigurationProperties(prefix = "oss")
public class OssUpLoadService implements UploadService {
    @Override
    public JsonResult uploadImg(MultipartFile img){
        //判断文件类型
        //获取原始文件名
        String originalFilename = img.getOriginalFilename();
        System.out.println(1);
       //对原始文件名进行判断
        if(!originalFilename.endsWith(".png")&&!originalFilename.endsWith(".jpg")&&!originalFilename.endsWith(".jpeg")){
            return new JsonResult(2,"上传的图片文件类型（png/jpg/jepg）有误！");
        }
        System.out.println(2);
       //如果判断通过上传文件到OSS
        String filePath = PathUtils.generateFilePath(originalFilename);

        String url = uploadOss(img,filePath);//  2099/2/3eqeqe.png
        System.out.println(url);
        return JsonResult.okResult(url);
    }

    private String accessKey ="8dr3prJBOtSiRPjJzPh2penU16O_3RnNkHJ80QMy";
    private String secretKey = "UkW0-Quxh7WE1OKcLCWLKYIxKpM-raTNXUzYOgy7";
    private String bucket = "hhyhhy";


    private String uploadOss(MultipartFile imgFile,String filePath){
    	System.out.println(3);
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.autoRegion());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = filePath;
        //String key = "fj";
        try {
        	System.out.println(4);
            InputStream inputStream = imgFile.getInputStream();
            System.out.println(inputStream);
            Auth auth = Auth.create(accessKey, secretKey);
            System.out.println(auth);
            String upToken = auth.uploadToken(bucket);
            try {
            	System.out.println(5);
                Response response = uploadManager.put(inputStream,key,upToken,null, null);
                //解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                System.out.println(putRet.key);
                System.out.println(putRet.hash);
                return "http://rd78qndek.hn-bkt.clouddn.com/"+key;
                //return "sss";
            } catch (QiniuException ex) {
            	System.out.println(6);
                Response r = ex.response;
                System.err.println(r.toString());
                try {
                    System.err.println(r.bodyString());
                } catch (QiniuException ex2) {
                    //ignore
                }
            }
        } catch (Exception ex) {
        	 System.out.println(ex);
        }
        return "www";
    }
}
